<!-- 2017-12-4 14:24:12 | 修改 木遥（微信:  http://marsgis.cn/weixin.html ） -->
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="x5-fullscreen" content="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
    <!-- 标题及搜索关键字 -->
    <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
    <meta
      name="description"
      content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
    />

    <link rel="shortcut icon" type="image/x-icon" href="http://mars3d.cn/favicon.ico" />
    <title>剖面分析 | Mars3D | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>

    <script
      type="text/javascript"
      src="../lib/include-lib.js"
      libpath="../lib/"
      include="jquery,font-awesome,bootstrap,layer,haoutil,turf,mars3d,echarts"
    ></script>

    <link href="css/style.css" rel="stylesheet" />
    <style>
      .sectionChars {
        top: auto;
        width: 90%;
        height: 200px;
        bottom: 30px;
        left: 5%;
        top: auto;
        bottom: 10px;
        display: none;
      }
    </style>
  </head>

  <body class="dark">
    <!--加载前进行操作提示，优化用户体验-->
    <div id="mask" class="signmask" onclick="removeMask()"></div>

    <div id="mars3dContainer" class="mars3d-container"></div>

    <!-- 面板 -->
    <div class="infoview">
      <span style="color: #cad1d1">提示：单击下面按钮激活绘制，按提示操作即可。</span>
      <br />

      <input type="button" class="btn btn-primary" value="剖面" onclick="measureSection()" />
      <input type="button" class="btn btn-info" value="清除" onclick="removeAll()" />
    </div>

    <div id="sectionChars" class="infoview sectionChars">
      <div id="echartsView1" style="width: 100%; height: 100%"></div>
    </div>

    <!-- 切换视角到模型或地形（山区）的快捷按钮 -->
    <script type="text/javascript" src="js/center_terrain_3dtiles.js"></script>

    <script src="./js/common.js"></script>
    <script type="text/javascript">
      'use script' //开发环境建议开启严格模式

      var map
      var measureObj

      function initMap(options) {
        //合并属性参数，可覆盖config.json中的对应配置
        var mapOptions = mars3d.Util.merge(options, {
          scene: {
            center: { y: 30.715648, x: 116.300527, z: 10727.2, heading: 2.9, pitch: -24.6, roll: 0 },
          },
        })

        //创建三维地球场景
        map = new mars3d.Map('mars3dContainer', mapOptions)

        measureObj = new mars3d.thing.Measure({
          //设置文本样式
          label: {
            color: '#ffffff',
            font_family: '楷体',
            font_size: 20,
          },
        })
        map.addThing(measureObj)

        measureObj.on(mars3d.EventType.start, function (e) {
          //开始分析前回调(异步)
          haoutil.loading.show()
        })
        measureObj.on(mars3d.EventType.end, function (e) {
          //分析完成后回调(异步)
          haoutil.loading.hide()
          if (e.mtype === 'section') {
            setEchartsData(e)
          }
        })

        measureObj.on(mars3d.EventType.click, function (e) {
          console.log('单击了对象', e)
          let result = e.graphic?.measureResult
          if (result && result.mtype === 'section') {
            setEchartsData(result)
          }
        })

        map.on(mars3d.EventType.click, function (e) {
          hideTipMarker()
        })
      }

      function removeAll() {
        $('#sectionChars').hide()

        measureObj.clear()
        hideTipMarker()
      }

      function measureSection() {
        measureObj.section({
          // maxPointNum:2,
          splitNum: 300, //插值次数
        })
      }

      var tipGraphic
      function showTipMarker(point, z, inthtml) {
        var _position_draw = Cesium.Cartesian3.fromDegrees(point.lng, point.lat, z)

        if (!tipGraphic) {
          tipGraphic = new mars3d.graphic.BillboardEntity({
            name: '当前点',
            position: _position_draw,
            style: {
              image: 'img/marker/mark3.png',
              scale: 1,
              horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
              verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
              scaleByDistance: new Cesium.NearFarScalar(10000, 1.0, 500000, 0.2),
            },
          }).addTo(map.graphicLayer)
          tipGraphic._setPositionsToCallback()
        }
        tipGraphic._position_draw = _position_draw
        tipGraphic
          .bindPopup(inthtml, {
            anchor: [0, -20], //左右、上下的偏移像素值。
          })
          .openPopup()
      }

      function hideTipMarker() {
        if (!tipGraphic) {
          return
        }
        tipGraphic.remove(true)
        tipGraphic = null
      }

      //图表
      var myChart1

      //剖面分析 图表
      function setEchartsData(data) {
        if (data == null || data.arrPoint == null) {
          return
        }

        var arrPoint = data.arrPoint

        $('#sectionChars').show()
        if (myChart1 == null) {
          myChart1 = echarts.init(document.getElementById('echartsView1'), 'dark')
        }

        var option = {
          grid: {
            left: 10,
            right: 10,
            bottom: 10,
            containLabel: true,
          },
          dataZoom: [
            {
              type: 'inside',
              throttle: 50,
            },
          ],
          tooltip: {
            trigger: 'axis',
            //position: function (point, params, dom, rect, size) {
            //    return [10, 20];
            //},
            formatter: function (params) {
              var inhtml = ''
              if (params.length === 0) {
                hideTipMarker()
                return inhtml
              }

              var hbgd = params[0].value //海拔高度
              var point = arrPoint[params[0].dataIndex] //所在经纬度
              var len = haoutil.str.formatLength(Number(params[0].axisValue))
              var hbgdStr = haoutil.str.formatLength(Number(params[0].value))

              inhtml = `当前位置<br />
                            距起点：${len}<br />
                            海拔：<span style='color:${params[0].color};'>${hbgdStr}</span><br />
                            经度：${point.lng}<br />
                            纬度：${point.lat}`

              showTipMarker(point, hbgd, inhtml)

              return inhtml
            },
          },
          xAxis: [
            {
              name: '行程',
              type: 'category',
              boundaryGap: false,
              axisLine: {
                show: false,
              },
              axisLabel: {
                show: false,
              },
              data: data.arrLen,
            },
          ],
          yAxis: [
            {
              //name: '高度',
              type: 'value',
              min: getMinZ(arrPoint),
              axisLabel: {
                formatter: '{value} 米',
              },
            },
          ],
          series: [
            {
              name: '高程值',
              type: 'line',
              smooth: true,
              symbol: 'none',
              sampling: 'average',
              itemStyle: {
                normal: {
                  color: 'rgb(255, 70, 131)',
                },
              },
              areaStyle: {
                normal: {
                  color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 0,
                      color: 'rgb(255, 158, 68)',
                    },
                    {
                      offset: 1,
                      color: 'rgb(255, 70, 131)',
                    },
                  ]),
                },
              },
              data: data.arrHB,
            },
          ],
        }

        myChart1.setOption(option)
      }

      function getMinZ(arr) {
        var minz = 'dataMin'
        if (arr == null || arr.length === 0) {
          return minz
        }

        minz = arr[0].alt
        for (var i = 0; i < arr.length; i++) {
          if (arr[i].alt < minz) {
            minz = arr[i].alt
          }
        }
        return minz
      }
    </script>
  </body>
</html>
